//
//  TalkVedioEditView.m
//  talkDemo
//
//  Created by telen on 15/4/23.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkVedioEditView.h"
#import "TalkDefine.h"

@implementation TalkVedioEditView
{
    UIScrollView* scrollview;
    //
    UIButton* btnShare;
    CGPoint btnShareShow_pt;
    CGPoint btnShareUnshow_pt;
    //
    NSArray* themes;
}

@synthesize shareBtn = btnShare;
@synthesize saveBtn = btnSave;
//@synthesize uploadBtn = btnUpload;

+ (TalkVedioEditView*)talkVedioEditForThemes:(NSArray *)arr ratio:(CGFloat)ratio_w
{
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    CGRect frame = CGRectMake(0, height, 768/ratio_w, 256/ratio_w);
    //return [[TalkVedioEditView alloc] initWithFrame:frame editForThemes:arr ratio:ratio_w];
    return [[TalkVedioEditView alloc] initWithFrame:frame ratio:ratio_w];
}

- (instancetype)initWithFrame:(CGRect)frame ratio:(CGFloat)ratio_w
{
    if (self = [super initWithFrame:frame]) {
        self.ratio = ratio_w;
        //
        //
        UIView* blackkuang = [[UIView alloc] init];
        blackkuang.alpha = 0.6f;
        blackkuang.backgroundColor = [UIColor blackColor];
        [self addSubview:blackkuang];
        blackkuang.translatesAutoresizingMaskIntoConstraints = NO;
        [blackkuang lyt_alignToParent];
        //
        
        //Save to file
        btnSave = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnSave setImage:[UIImage imageNamed:@"talk_btn_save_normal.png"] forState:UIControlStateNormal];
        [btnSave setImage:[UIImage imageNamed:@"talk_btn_save_down.png"] forState:UIControlStateHighlighted];
        btnSave.frame = CGRectMake(0, 0, 90, 90);
        btnSave.center = CGPointMake(384/ratio_w, 128/ratio_w);
        btnSave.hidden = YES;
        [self addSubview:btnSave];
        [btnSave addTarget:self action:@selector(didClickedSaveBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnShare setImage:[UIImage imageNamed:@"talk_btn_share_normal.png"] forState:UIControlStateNormal];
        [btnShare setImage:[UIImage imageNamed:@"talk_btn_share_down.png"] forState:UIControlStateHighlighted];
        btnShare.frame = btnSave.frame;
        btnShare.hidden = YES;
        btnShare.center = btnSave.center;
        [self addSubview:btnShare];
        [btnShare addTarget:self action:@selector(didClickedShareBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        /*
        //Upload to server
        btnUpload = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnUpload setImage:[UIImage imageNamed:@"talk_btn_save_normal.png"] forState:UIControlStateNormal];
        [btnUpload setImage:[UIImage imageNamed:@"talk_btn_save_down.png"] forState:UIControlStateHighlighted];
        btnUpload.frame = CGRectMake(0, 0, 90, 90);
        btnUpload.center = CGPointMake(604/ratio_w, 128/ratio_w);
        btnUpload.hidden = YES;
        [self addSubview:btnUpload];
        [btnUpload addTarget:self action:@selector(didClickedUploadBtn:) forControlEvents:UIControlEventTouchUpInside];
         */
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame editForThemes:(NSArray *)arr ratio:(CGFloat)ratio_w
{
    if (self = [super initWithFrame:frame]) {
        self.ratio = ratio_w;
        themes = arr;
        btnShareShow_pt = CGPointMake(708/ratio_w, 40/ratio_w);
        btnShareUnshow_pt = CGPointMake(800/ratio_w, 40/ratio_w);
        //
        UIView* blackkuang = [[UIView alloc] init];
        blackkuang.alpha = 0.6f;
        blackkuang.backgroundColor = [UIColor blackColor];
        [self addSubview:blackkuang];
        blackkuang.translatesAutoresizingMaskIntoConstraints = NO;
        [blackkuang lyt_alignToParent];
        //
        scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        scrollview.backgroundColor = [UIColor clearColor];
        scrollview.showsHorizontalScrollIndicator = NO;
        scrollview.showsVerticalScrollIndicator = NO;
        [self addSubview:scrollview];
        //
        UIImage* image = [UIImage imageNamed:@"talk_btn_share_normal.png"];
        btnShare = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnShare setImage:image forState:UIControlStateNormal];
        [btnShare setImage:[UIImage imageNamed:@"talk_btn_share_down.png"] forState:UIControlStateHighlighted];
        btnShare.frame = CGRectMake(0, 0, image.size.width, image.size.height);
        btnShare.center = btnShareUnshow_pt;
        [btnShare addTarget:self action:@selector(didClickedShareBtn:) forControlEvents:UIControlEventTouchUpInside];
        //
        CGFloat pW = 120/ratio_w;
        CGFloat pH = frame.size.height;
        CGFloat pod = 30/ratio_w;
        NSString* keyName = @"name_en";
        if ([TalkLocalizedString(@"language", "") isEqualToString:@"cn"]) {
            keyName = @"name_cn";
        }
        for (int i=0; i<themes.count; i++) {
            CGRect ff = CGRectMake(i*(pW+pod)+pod*0.5, 0, pW, pH);
            NSDictionary* dict = [themes objectAtIndex:i];
            image = [UIImage imageWithContentsOfFile:[dict objectForKey:@"icon"]];
            TalkThemeChooseView* chooseView = [TalkThemeChooseView talkThemeChooseViewFrame:ff name:[dict objectForKey:keyName] image:image ratio:ratio_w];
            [scrollview addSubview:chooseView];
            chooseView.btn.tag = i;
            [chooseView.btn addTarget:self action:@selector(clickThemeBtn:) forControlEvents:UIControlEventTouchUpInside];
        }
        //more
        CGRect ff = CGRectMake(themes.count*(pW+pod)+pod*0.5, 0, pW, pH);
        image = [UIImage imageNamed:@"talk_more.png"];
        TalkThemeChooseView* chooseView = [TalkThemeChooseView talkThemeChooseViewFrame:ff name:TalkLocalizedString(@"more", "") image:image ratio:ratio_w];
        [scrollview addSubview:chooseView];
        chooseView.btn.tag = -1;
        [chooseView.btn addTarget:self action:@selector(clickThemeBtn:) forControlEvents:UIControlEventTouchUpInside];
        //
        CGFloat cw =  (themes.count+1)*(pW+pod)+pod*0.5;
        cw = frame.size.width+2 >= cw ? frame.size.width+2 : cw;
        CGSize csize = CGSizeMake(cw, frame.size.height);
        [scrollview setContentSize:csize];
    }
    return self;
}

- (void)clickThemeBtn:(UIButton*)btn
{
    if(btn.tag >= 0)
    {
        if(self.delegate != nil && [self.delegate respondsToSelector:@selector(TalkVideoEdit_ThemeSelected:)])
            [self.delegate TalkVideoEdit_ThemeSelected:[themes objectAtIndex:btn.tag]];
    }
}

-(void)didClickedShareBtn:(UIButton*)sender
{
    if(self.delegate != nil && [self.delegate respondsToSelector:@selector(TalkVideoEdit_Share:)])
        [self.delegate TalkVideoEdit_Share:sender];
}

- (void)didClickedSaveBtn:(UIButton*)sender
{
    if(self.delegate != nil && [self.delegate respondsToSelector:@selector(TalkVideoEdit_Save)])
        [self.delegate TalkVideoEdit_Save];
}

- (void)didClickedUploadBtn:(UIButton*)sender
{
    if(self.delegate != nil && [self.delegate respondsToSelector:@selector(TalkVideoEdit_Upload)])
        [self.delegate TalkVideoEdit_Upload];
}

- (void)showBtnSave:(BOOL)yn
{
    if (yn) {
        btnSave.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.3 animations:^{
                btnSave.hidden = NO;
                btnSave.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
                btnSave.transform = CGAffineTransformMakeScale(1, 1);
            }];
        } completion:^(BOOL finished) {
            btnSave.hidden = NO;
//            btnUpload.hidden = NO;
        }];
    }else{
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
                btnSave.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
                btnSave.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            }];
        } completion:^(BOOL finished) {
            btnSave.hidden = YES;
 //           btnUpload.hidden = YES;
        }];
    }
}

- (void)showBtnShare:(BOOL)yn
{
    if (yn) {
        btnShare.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.3 animations:^{
                btnShare.hidden = NO;
                btnShare.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.7 relativeDuration:0.3 animations:^{
                btnShare.transform = CGAffineTransformMakeScale(1, 1);
            }];
        } completion:^(BOOL finished) {
            btnShare.hidden = NO;
            //            btnUpload.hidden = NO;
        }];
    }else{
        [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
            [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
                btnShare.transform = CGAffineTransformMakeScale(1.1f, 1.1f);
            }];
            [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.2 animations:^{
                btnShare.transform = CGAffineTransformMakeScale(0.0001f, 0.0001f);
            }];
        } completion:^(BOOL finished) {
            btnShare.hidden = YES;
            //           btnUpload.hidden = YES;
        }];
    }
}

- (void)show
{
    CGRect frame = self.frame;
    if (!btnShare.superview) {
        [self.superview addSubview:btnShare];
    }
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height-256/self.ratio);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            self.frame = frame;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.6 relativeDuration:0.3 animations:^{
            //btnShare.center = btnShareShow_pt;
            [self showBtnSave:!_showShareBtn];
            [self showBtnShare:_showShareBtn];
        }];
    } completion:^(BOOL finished) {
    }];
    
    self.isShown = YES;
}

- (void)unshow
{
    CGRect frame = self.frame;
    CGSize ssize = [UIScreen mainScreen].bounds.size;
    CGFloat height = ssize.height > ssize.width ? ssize.height : ssize.width;
    frame.origin = CGPointMake(frame.origin.x, height);
    [UIView animateKeyframesWithDuration:1.0f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.6 relativeDuration:0.3 animations:^{
            self.frame = frame;
            //btnShare.center = btnShareUnshow_pt;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            //btnShare.center = btnShareShow_pt;
            [self showBtnSave:NO];
            [self showBtnShare:NO];
        }];
    } completion:^(BOOL finished) {
        
    }];
    
    self.isShown = NO;
}


@end





@implementation TalkThemeChooseView
{
    UIImageView* imageView;
}

+ (TalkThemeChooseView *)talkThemeChooseViewFrame:(CGRect)frame name:(NSString *)name image:(UIImage *)image ratio:(CGFloat)ratio_w
{
    return [[TalkThemeChooseView alloc] initWithFrame:frame name:name image:image ratio:ratio_w];
}

- (instancetype)initWithFrame:(CGRect)frame name:(NSString *)name image:(UIImage *)image ratio:(CGFloat)ratio_w
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        //
        CGFloat lHeiht = 40/ratio_w;
        CGFloat d = frame.size.width <= frame.size.height-lHeiht ? frame.size.width : frame.size.height-lHeiht;
        //
        UIImageView* igv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, d, d)];
        igv.backgroundColor = [UIColor whiteColor];
        igv.image = image;
        igv.center = CGPointMake(frame.size.width*0.5, (frame.size.height-lHeiht)*0.5);
        [self addSubview:igv];
        [igv.layer setCornerRadius:d*0.5];
        [igv.layer setBorderColor:[UIColor whiteColor].CGColor];
        [igv.layer setBorderWidth:4/ratio_w];
        igv.layer.masksToBounds = YES;
        imageView = igv;
        //
        UILabel* lb = [[UILabel alloc] initWithFrame:CGRectMake(0, igv.frame.size.height+igv.frame.origin.y, frame.size.width, lHeiht)];
        lb.text = name;
        lb.font = [UIFont systemFontOfSize:lHeiht*0.5];
        lb.textColor = [UIColor whiteColor];
        lb.textAlignment = NSTextAlignmentCenter;
        [self addSubview:lb];
        //
        frame.origin = CGPointZero;
        _btn = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn.frame = frame;
        [self addSubview:_btn];
        [_btn addObserver:self forKeyPath:@"highlighted" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

- (void)dealloc
{
    [_btn removeObserver:self forKeyPath:@"highlighted"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"highlighted"]) {
        if (_btn.state == UIControlStateHighlighted) {
            [imageView.layer setBorderColor:__TalkBaseBackColor.CGColor];
        }else{
            [imageView.layer setBorderColor:[UIColor whiteColor].CGColor];
        }
    }
}

@end
